package com.doraemon.service.impl;

import com.doraemon.constant.StatusConstant;
import com.doraemon.entity.Orders;
import com.doraemon.mapper.DishMapper;
import com.doraemon.mapper.OrderMappr;
import com.doraemon.mapper.SetmealMapper;
import com.doraemon.mapper.UserMapper;
import com.doraemon.service.WorkSpaceService;
import com.doraemon.vo.BusinessDataVO;
import com.doraemon.vo.DishOverViewVO;
import com.doraemon.vo.OrderOverViewVO;
import com.doraemon.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

/**
 * 工作台管理Service实现
 **/
@Service
public class WorkSpaceServiceImpl implements WorkSpaceService {

    @Autowired
    private OrderMappr orderMappr;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /***
     * 根据时间段统计营业数据
     * ---
     * @param begin 开始时间
     * @param end 结束时间
     * @return: com.doraemon.vo.BusinessDataVO
     */
    @Override
    public BusinessDataVO getBusinessData(LocalDateTime begin, LocalDateTime end) {

        // 1.创建Map集合，存放开始时间与结束时间
        Map map = new HashMap();
        map.put("begin", begin);
        map.put("end", end);

        // 2.查询总订单数
        Integer totalOrderCount = orderMappr.countByMap(map);

        // 3.查询营业额
        map.put("status", Orders.COMPLETED); // 设置订单状态为完成
        Double turnover = orderMappr.sumByMap(map);
        turnover = turnover == null ? 0.0 : turnover;

        // 4.查询有效订单数
        Integer validOrderCount = orderMappr.countByMap(map);

        // 5.查询订单完成率与平均客单价
        Double unitPrice = 0.0; // 平均客单价
        Double orderCompletionRate = 0.0; // 订单完成率
        if (totalOrderCount != 0 && validOrderCount != 0) { // 总订单数与有效订单不为0时
            orderCompletionRate = validOrderCount.doubleValue() / totalOrderCount;
            unitPrice = turnover / validOrderCount;
        }

        // 6.查询新增用户数
        Integer newUsers = userMapper.countByMap(map);

        // 7.封装运营数据VO类
        BusinessDataVO businessDataVO = new BusinessDataVO();
        businessDataVO.setTurnover(turnover);
        businessDataVO.setUnitPrice(unitPrice);
        businessDataVO.setOrderCompletionRate(orderCompletionRate);
        businessDataVO.setNewUsers(newUsers);
        businessDataVO.setValidOrderCount(validOrderCount);

        return businessDataVO;
    }

    /***
     * 查询订单管理数据
     * ---
     * @return: com.doraemon.result.Result<com.doraemon.vo.OrderOverViewVO>
     */
    @Override
    public OrderOverViewVO getOrderOverView() {

        // 1.创建Map集合，存放开始时间与订单状态
        Map map = new HashMap();
        map.put("begin", LocalDateTime.now().with(LocalTime.MIN));
        map.put("status", Orders.TO_BE_CONFIRMED);

        // 2.获取待接单状态的订单
        Integer waitingOrders = orderMappr.countByMap(map);

        // 3.获取待派送状态的订单
        map.put("status", Orders.CONFIRMED);
        Integer deliveredOrders = orderMappr.countByMap(map);

        // 4.获取已完成状态的订单
        map.put("status", Orders.COMPLETED);
        Integer completedOrders = orderMappr.countByMap(map);

        // 5.获取已取消状态的订单
        map.put("status", Orders.CANCELLED);
        Integer cancelledOrders = orderMappr.countByMap(map);

        // 6.获取全部订单
        map.put("status", null);
        Integer allOrders = orderMappr.countByMap(map);

        // 7.封装订单管理数据VO类
        OrderOverViewVO orderOverViewVO = new OrderOverViewVO();
        orderOverViewVO.setWaitingOrders(waitingOrders);
        orderOverViewVO.setDeliveredOrders(deliveredOrders);
        orderOverViewVO.setCompletedOrders(completedOrders);
        orderOverViewVO.setCancelledOrders(cancelledOrders);
        orderOverViewVO.setAllOrders(allOrders);

        return orderOverViewVO;
    }

    /***
     * 查询菜品总览
     * ---
     * @return: com.doraemon.result.Result<com.doraemon.vo.DishOverViewVO>
     */
    @Override
    public DishOverViewVO getDishOverView() {

        // 1.创建Map集合，存放菜品状态
        Map map = new HashMap();
        map.put("status", StatusConstant.ENABLE); // 菜品状态为启用

        // 2.调用菜品Mapper层，获取启用的菜品数量
        Integer sold = dishMapper.countByMap(map);

        // 3.调用菜品Mapper层，获取禁用的菜品数量
        map.put("status", StatusConstant.DISABLE); // 菜品状态为禁用
        Integer discontinued = dishMapper.countByMap(map);

        // 4.封装菜品总览VO类
        DishOverViewVO dishOverViewVO = new DishOverViewVO();
        dishOverViewVO.setSold(sold);
        dishOverViewVO.setDiscontinued(discontinued);

        return dishOverViewVO;
    }

    /***
     * 查询套餐总览
     * ---
     * @return: com.doraemon.result.Result<com.doraemon.vo.SetmealOverViewVO>
     */
    @Override
    public SetmealOverViewVO getSetmealOverView() {

        // 1.创建Map集合，存放套餐状态
        Map map = new HashMap();
        map.put("status", StatusConstant.ENABLE);

        // 2.调用套餐Mapper层，获取启用的套餐数量
        Integer sold = setmealMapper.countByMap(map);

        // 3.调用套餐Mapper层，获取禁用的套餐数量
        map.put("status", StatusConstant.DISABLE);
        Integer discontinued = setmealMapper.countByMap(map);

        // 4.封装套餐总览VO类
        SetmealOverViewVO setmealOverViewVO = new SetmealOverViewVO();
        setmealOverViewVO.setSold(sold);
        setmealOverViewVO.setDiscontinued(discontinued);

        return setmealOverViewVO;
    }

}
